package com.next

class Campaign
{
    String name
    Boolean active = true
    Campaign parent
    CampaignType type
    CampaignStatus status

    User createBy
    User modifyBy

    User user

    Date startTime
    Date endTime

    String description

    Territory territory

    Date createdDate = new Date()
    Date modifiedDate = new Date()

    static hasMany = [notes: Note, attachments: Attachment, activities: Activity, users: CampaignTeam]

    static constraints = {
        territory nullable: true, blank: true
        createBy nullable: true, blank: true
        modifyBy nullable: true, blank: true
        createdDate nullable: true, blank: true
        modifiedDate nullable: true, blank: true
        user nullable: true, blank: true
    }

    String toString()
    {
        name
    }

    def springSecurityService

    def beforeInsert()
    {
        def username = springSecurityService?.getPrincipal()?.username
        createBy = User.findByUsername(username)
        if (!user)
        {
            user = createBy
        }

        // 初始化 所有者 市场权限
        CampaignTeam campaignTeam = new CampaignTeam(campaign: this, user: user, teamRole: TeamRole.findByName('Edit'),
                                                     contactPermission: TeamRole.findByName('Edit'),
                                                     activityPermission: TeamRole.findByName('Edit'),
                                                     teamPermission: TeamRole.findByName('Edit'),
                                                     attachmentPermission: TeamRole.findByName('Edit'),
                                                     notePermission: TeamRole.findByName('Edit'),
                                                     flexFieldPermission: TeamRole.findByName('Edit'),
                                                     addressPermission: TeamRole.findByName('Edit'))
        this.addToUsers(campaignTeam)

        // 初始化 上级用户 市场权限
        if (user.shareCampaignToSupervisor)
        {
            User reportTo = user.reportTo

            while (reportTo)
            {

                CampaignTeam campaignTeam1 = new CampaignTeam(campaign: this, user: reportTo, teamRole: TeamRole.findByName('Read'),
                                                              contactPermission: TeamRole.findByName('Read'),
                                                              activityPermission: TeamRole.findByName('Read'),
                                                              teamPermission: TeamRole.findByName('Read'),
                                                              attachmentPermission: TeamRole.findByName('Read'),
                                                              notePermission: TeamRole.findByName('Read'),
                                                              flexFieldPermission: TeamRole.findByName('Read'),
                                                              addressPermission: TeamRole.findByName('Read'))

                this.addToUsers(campaignTeam1)

                reportTo = reportTo.reportTo
            }
        }
    }

    def afterInsert()
    {
        def flexFieldCategories = FlexFieldCategory.findAllByObjectType('Campaign')
        flexFieldCategories?.each {
            if (it?.condition)
            {
                def result = false
                try
                {
                    def shell = new GroovyShell()
                    def closure = shell.evaluate(it?.condition?.script)
                    result = closure(this)
                }
                catch (Exception e)
                {
                    log.error e
                }
                if (result)
                {
                    initFlexField(it)
                }
            }
            else
            {
                initFlexField(it)
            }
        }

    }

    def initFlexField(FlexFieldCategory flexFieldCategory)
    {
        def fields = FlexField.findAllByCategory(flexFieldCategory)
        fields?.each {
            def field = new FlexFieldInstance(category: flexFieldCategory, field: it, campaign: this)
            field.save()
        }
    }

    def beforeUpdate()
    {
        modifiedDate = new Date()
        def username = springSecurityService?.getPrincipal()?.username
        modifyBy = User.findByUsername(username)
    }
}
